//
//  HomeDeviceCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa

open class HomeDeviceCell: HomeBaseCell {
    
   
    let addButton = Button(type: .custom)
    
    public override func makeUI() {
        super.makeUI()
        
        dateLabel.isHidden = true
//        bgView.isUserInteractionEnabled = false
        bgView.addSubview(addButton)
        
        addButton.rx.tap.subscribe(onNext: {[weak self] _ in
            
            self?.selectedItem?()
            
        }).disposed(by: rx.disposeBag)
        
        titleLabel.numberOfLines = 0
        titleLabel.textColor = UIColor.assist_222222
        titleLabel.font = Fonts.systemFont(ofSize: 16)
        
        addButton.backgroundColor = UIColor.theme
        addButton.setTitleColor(UIColor.assist_FFFFFF, for: .normal)
        addButton.cornerRadius = 17.0
        addButton.titleLabel?.font = Fonts.systemFont(ofSize: 12)
        
      
        iconImagView.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.top.equalToSuperview().offset(17)
            make.width.equalTo(10.5)
            make.height.equalTo(10.5)
        }
        
        titleLabel.snp.remakeConstraints { make in
            make.left.equalTo(iconImagView.snp.right).offset(5)
            make.right.lessThanOrEqualToSuperview().offset(-15)
            make.top.equalTo(13)
        }
        
        addButton.snp.makeConstraints { make in
           
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(titleLabel.snp.bottom).offset(14)
            make.bottom.equalTo(bgView.snp.bottom).offset(-10).priority(999)
            make.height.equalTo(34)
            
            
        }
        
    }
    
    public override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeDeviceViewModel else {
            return
        }
        
        let disposeBag = DisposeBag()
        
        viewModel.modelRelay.subscribe(onNext: {[weak self] in
            
//            print($0.title)
                
            self?.iconImagView.image = UIImage(named: $0.iconName)
            self?.titleLabel.text = $0.title
            self?.addButton.setTitle($0.buttonTitle, for: .normal)
            
            
            
        }).disposed(by: disposeBag)
        
       
    }
    
//    open override func layoutSubviews() {
//        super.layoutSubviews()
//        
//        var frame = contentView.frame
//        if frame.height < 110 {
//            frame.size.height = 110
//            contentView.frame = frame
//        }
//        
//        
//        
//    }
    
    

}
